package cn.com.ddapp.ddm.ui.mine;

import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.InfoCenterAdapter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.mine.InformationEntity;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.widget.pulltorefresh.PullToRefreshLayout;
import cn.com.ddapp.ddm.widget.pulltorefresh.pullableview.PullableListView;

public class InfoCenterActivity extends BaseActivity {
    @BindView(R.id.lv_info)
    PullableListView mListView;
    @BindView(R.id.layout_refresh)
    PullToRefreshLayout layoutRefresh;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;

    private String userId;
    private int st = 0;
    List<InformationEntity.ListBean> mylist = new ArrayList<>();
    private InfoCenterAdapter adapter;
    //横竖屏
    int orientation;
    DisplayMetrics dm;

    @Override
    public void initView() {
        titletext.setText("消息中心");
        orientation = getResources().getConfiguration().orientation;
        dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        userId = SPUtils.getStringData(this, SPUtils.USERID);
        st = 0;
        requestData(0);
        //设置监听
        setListener();
    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_info_center;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    private void setListener() {
        //上下拉刷新监听
        layoutRefresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                //刷新
                st = 0;//设置页码为0
                requestData(st);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                requestLoadData(st);
            }
        });

        //listview点击监听
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //跳转到详情页，将id,userid传过去
                Intent intent = new Intent(InfoCenterActivity.this, InfoDetailsActivity.class);
                intent.putExtra("id", mylist.get(position).getId());
//                intent.putExtra("id", list.get(position).getId());
                startActivity(intent);
            }
        });
    }

    private void requestData(final int s) {
        mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        final OkUtilCallback okUtilCallback = new OkUtilCallback(mEmptyLayout) {
            @Override
            public void onMyResponse(String response, int id) {
                InformationEntity bean = gson.fromJson(response, InformationEntity.class);
                if (!bean.getResult().equals("OK")) {
                    BaseApplication.getIntstance().showToast(bean.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                } else if (bean.getResult().equals("OK") && bean.getList().size() == 0) {
                    mEmptyLayout.setErrorType(EmptyLayout.NODATA);
                } else {
                    mylist = bean.getList();
                    st = s + mylist.size();
                    adapter = new InfoCenterAdapter(InfoCenterActivity.this, mylist,orientation,dm);
                    mListView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    layoutRefresh.refreshFinish(PullToRefreshLayout.SUCCEED);
                }
            }
        };
        String params = "userId=" + userId + "&st=" + s;
        OKUtilAPI.post(OKUtilAPI.mlist, params, okUtilCallback);
    }

    private void requestLoadData(final int s) {
        mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        final OkUtilCallback okUtilCallback = new OkUtilCallback(mEmptyLayout) {
            @Override
            public void onMyResponse(String response, int id) {
                InformationEntity bean = gson.fromJson(response, InformationEntity.class);
                List<InformationEntity.ListBean> sList = bean.getList();
                if (sList.size() > 0) {
                    mylist.addAll(sList);
                    adapter.notifyDataSetChanged();
                    layoutRefresh.refreshFinish(PullToRefreshLayout.SUCCEED);
                    st = s + sList.size();

                } else {
                    layoutRefresh.loadmoreFinish(PullToRefreshLayout.NODATA);
                }
            }
        };
        String params = "userId=" + userId + "&st=" + st;
        OKUtilAPI.post(OKUtilAPI.mlist, params, okUtilCallback);
    }

    @OnClick({R.id.img_error_layout,R.id.back})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_error_layout:
                requestData(0);
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }
}
